<?php

namespace App\Models;

use Dcat\Admin\Models\Administrator;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class TeachingMaterial extends Model
{
    use HasFactory;
    use HasDateTimeFormatter;


    public static function boot()
    {
        parent::boot();

        static::created(function (self $item) {
            MaterialUnit::factory()->count(intval($item->number))->create([
                'book_id' => $item->id,
                'mechanism_id' => $item->mechanism_id
            ]);
            $item->number = intval($item->number);
            $item->save();
        });

    }

    public function mechanism()
    {
        return $this->belongsTo(Administrator::class, 'mechanism_id');
    }

    public function units()
    {
        return $this->hasMany(MaterialUnit::class, 'book_id');
    }
}
